<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var rearrangeArray = function (nums) {
      // 先按照大于 0 和小于 0 的分类
      const r = []
      const l = []
      for (let i = 0; i < nums.length; i++) {
        if (nums[i] < 0) {
          l.push(nums[i])
        } else {
          r.push(nums[i])
        }
      }
      // 然后插花拼到一起即可
      const tar = []
      for (let i = 0; i < r.length; i++) {
        tar.push(r[i], l[i])
      }
      return tar
    };
    console.log(rearrangeArray(nums = [3, 1, -2, -5, 2, -4]))
  </script>
</body>

</html>